package com.t235.gc.device.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.t235.gc.AjaxResult;
import com.t235.gc.device.entity.Device;
import com.t235.gc.device.entity.vo.DeviceVo;
import com.t235.gc.device.service.DeviceService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 设备表(Device)表控制层
 *
 * @author makejava
 * @since 2021-03-24 08:58:28
 */
@RestController
@RequestMapping("device")
public class DeviceController {
    /**
     * 服务对象
     */
    @Resource
    private DeviceService deviceService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Device selectOne(Long id) {
        return this.deviceService.queryById(id);
    }

    @GetMapping("selectAll")
    @ApiOperation("设备列表条件查询信息")
    public AjaxResult selectAll(Device device,@RequestParam(value = "page")Integer pageNum,@RequestParam(value = "limit")Integer pageSize) {
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 5;
        }
        System.out.println(device.getDevicenumber());
        PageHelper.startPage(pageNum, pageSize);
        List<DeviceVo> list = deviceService.queryAll(device);
        PageInfo<DeviceVo> pageInfo = new PageInfo<>();
        pageInfo.setList(list);
        pageInfo.setSize(deviceService.queryCount(device));
        return AjaxResult.success(pageInfo);
    }

    @PutMapping("update")
    public AjaxResult update(@RequestBody Device device){
        Device device1 = deviceService.update(device);
        if(device1 != null){
            return AjaxResult.success(device1);
        }
        return AjaxResult.error("10001");
    }

    @PostMapping("add")
    public AjaxResult add(@RequestBody Device device){
        Device device1 = deviceService.insert(device);
        if(device1 != null){
            return AjaxResult.success(device1);
        }
        return AjaxResult.error("10001");
    }

    @DeleteMapping("delete/{id}")
    public AjaxResult delete(@PathVariable Long id){
        boolean bon = deviceService.deleteById(id);
            return AjaxResult.success(bon);
    }

    @GetMapping("queryDevicecount")
    public AjaxResult queryDevicecount(){
        int num = deviceService.queryDevicecount();
        return AjaxResult.success(num);
    }

    @GetMapping("queryDevicecountBystatus/{status}")
    public AjaxResult queryDevicecountBystatus(@PathVariable int status){
        int num = deviceService.queryDevicecountBystatus(status);
        return AjaxResult.success(num);
    }

    @GetMapping("queryisStock")
    public AjaxResult queryisStock(){
        int num = deviceService.queryisStock();
        return AjaxResult.success(num);
    }

    @RequestMapping("/exportExcel")
    public AjaxResult exportExcel(Device device, HttpServletResponse response){
        deviceService.exportExcel(device,response);
        return AjaxResult.success();
    }
}
